package Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.homework1.Message;
import com.example.homework1.R;

import java.util.List;

public class Myadapter extends RecyclerView.Adapter<Myadapter.ViewHolder>{

    private List<Message> Message_list;

    public Myadapter(List<Message> templist){
        Message_list = templist;
    }
    static  class ViewHolder extends RecyclerView.ViewHolder{
        View myview;
        ImageButton image;
        Button message;
        Button data;
        ConstraintLayout clayout;
        public ViewHolder(View view){
            super(view);
            image = (ImageButton) view.findViewById(R.id.image);
            message = (Button) view.findViewById(R.id.line_button1);
            data = (Button)view.findViewById(R.id.line_button2);
            clayout = (ConstraintLayout)view.findViewById(R.id.clayout);

        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_line2,parent,false);
       final ViewHolder holder = new ViewHolder(view);
       holder.clayout.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               int position = holder.getAdapterPosition();
               Message m = Message_list.get(position);
               Toast.makeText(v.getContext(),"you click the line button"+m.getName(),Toast.LENGTH_SHORT).show();

           }
       });
       return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Message message = Message_list.get(position);
        //holder.image.setImageResource();
        holder.message.setText(message.getName());
        holder.data.setText(message.getData());

    }



    @Override
    public int getItemCount() {
        return Message_list.size();
    }
}
